package org.penguin.study.bk.mq.controller;

import org.penguin.study.bk.base.api.Return;
import org.penguin.study.bk.base.util.IdUtil;
import org.penguin.study.bk.config.RabbitMqConfig;
import org.penguin.study.bk.mq.model.RabbitQueue1Message;
import org.penguin.study.bk.mq.model.RabbitQueue2Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @author chengyinghua
 */
@RestController
@RequestMapping("/mq/rabbit")
@ConditionalOnProperty(name = "rabbitmq.enable", havingValue = "true")
public class RabbitMqController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/publish")
    public Return<?> publishMessage(@RequestParam(name = "message", defaultValue = "RabbitMQ入门经典!") String message, @RequestParam(defaultValue = "1") String queue) {
        if ("1".equals(queue)) {
            RabbitQueue1Message rabbitMessage = new RabbitQueue1Message(IdUtil.generateUUID(), "Penguin组织", message, LocalDateTime.now());
            rabbitTemplate.convertAndSend(RabbitMqConfig.topicExchangeName, RabbitMqConfig.queue1RoutingKey, rabbitMessage);
        } else if ("2".equals(queue)) {
            RabbitQueue2Message rabbitMessage = new RabbitQueue2Message(IdUtil.generateUUID(), "Penguin组织", message, LocalDateTime.now());
            rabbitTemplate.convertAndSend(RabbitMqConfig.topicExchangeName, RabbitMqConfig.queue2RoutingKey, rabbitMessage);
        }
        return Return.Companion.ok("成功给RabbitMQ发消息");
    }
}
